package com.haixiaoke.saas.messageCenter.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.haixiaoke.saas.common.constant.MessageConstants;
import com.haixiaoke.saas.common.enums.PushConditionEnum;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.messageCenter.domain.PushRecord;
import com.haixiaoke.saas.messageCenter.mapper.PushRecordMapper;
import com.haixiaoke.saas.messageCenter.service.IPushRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 推送记录信息Service业务层处理
 *
 * @author xiaolong
 * @date 2024-11-19
 */
@Service
public class PushRecordServiceImpl implements IPushRecordService {
    @Autowired
    private PushRecordMapper pushRecordMapper;

    /**
     * 查询推送记录信息
     *
     * @param pushRecordId 推送记录信息主键
     * @return 推送记录信息
     */
    @Override
    public PushRecord selectPushRecordByPushRecordId(Long pushRecordId) {
        return pushRecordMapper.selectPushRecordByPushRecordId(pushRecordId);
    }

    /**
     * 查询推送记录信息列表
     *
     * @param pushRecord 推送记录信息
     * @return 推送记录信息
     */
    @Override
    public List<PushRecord> selectPushRecordList(PushRecord pushRecord) {
        return pushRecordMapper.selectPushRecordList(pushRecord);
    }

    /**
     * 新增推送记录信息
     *
     * @param pushRecord 推送记录信息
     * @return 结果
     */
    @Override
    public int insertPushRecord(PushRecord pushRecord) {
        pushRecord.setCreateTime(DateUtils.getNowDate());
        return pushRecordMapper.insertPushRecord(pushRecord);
    }

    /**
     * 修改推送记录信息
     *
     * @param pushRecord 推送记录信息
     * @return 结果
     */
    @Override
    public int updatePushRecord(PushRecord pushRecord) {
        pushRecord.setUpdateTime(DateUtils.getNowDate());
        return pushRecordMapper.updatePushRecord(pushRecord);
    }

    /**
     * 批量删除推送记录信息
     *
     * @param pushRecordIds 需要删除的推送记录信息主键
     * @return 结果
     */
    @Override
    public int deletePushRecordByPushRecordIds(Long[] pushRecordIds) {
        return pushRecordMapper.deletePushRecordByPushRecordIds(pushRecordIds);
    }

    /**
     * 删除推送记录信息信息
     *
     * @param pushRecordId 推送记录信息主键
     * @return 结果
     */
    @Override
    public int deletePushRecordByPushRecordId(Long pushRecordId) {
        return pushRecordMapper.deletePushRecordByPushRecordId(pushRecordId);
    }

    @Override
    public int insertBatchPushRecordList(ArrayList<PushRecord> pushRecordList) {
        return pushRecordMapper.insertBatchPushRecordList(pushRecordList);
    }

    @Override
    public PushRecord getInfo(Long pushRecordId) {
        PushRecord pushRecord = pushRecordMapper.selectPushRecordByPushRecordId(pushRecordId);
        if (ObjectUtil.isEmpty(pushRecord)) {
            throw new ServiceException("推送记录不存在");
        }
        if (MessageConstants.TRIGGERING_BEHAVIOR_CONDITION_PUSH.equals(pushRecord.getTriggeringBehavior())) {
            pushRecord.setConditionName(PushConditionEnum.getLabelByCode(pushRecord.getPushConditionCode()));
        }
        return pushRecord;
    }

    @Override
    public int getStatisticsByTenantId(String tenantId) {
        return pushRecordMapper.getStatisticsByTenantId(tenantId);
    }

    @Override
    public int getStatisticsByUserId(Long userId) {
        return pushRecordMapper.getStatisticsByUserId(userId);
    }
}
